﻿using Core.SimpleTemp.Application.WebPageInitialApp.Model;
using Core.SimpleTemp.Common.AutoMapperExtend;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization.Help;
using Core.Toolkit.Common;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [EnableCors("any")]
    [ApiExplorerSettings(GroupName = "Sys")]
    [ApiController]
    [Authorize]
    public class WebPageInitialController : CoreApiController
    {
        private readonly IDataDicHelp _dataDicHelp;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="dataDicHelp"></param>
        public WebPageInitialController(IDataDicHelp dataDicHelp)
        {
            _dataDicHelp = dataDicHelp;
        }

        /// <summary>
        ///  获取所有除总部外的其他工厂信息当前登录人为总部，全部返回，否则仅返回对应工厂
        /// </summary>
        /// <returns></returns>
        private List<KeyValue<string, string>> GetFactoryList()
        {
            var list = _dataDicHelp.GeDataDicValueByCategory("9001");
            //总部人员返回所有工厂
            if (UserIdentity.Headoffice)
                return list;
            else
                return list.Where(a => a.Key == UserIdentity?.Factory?.ToString()).ToList();
        }

        /// <summary>
        /// 获取所有工厂信息，包括总部，当前登录人为总部，全部返回，否则仅返回对应工厂
        /// </summary>
        /// <returns></returns>
        private List<KeyValue<string, string>> GetFactoryListAll()
        {
            var list = _dataDicHelp.GeDataDicValueByCategory("9002");
            //总部人员返回所有工厂
            if (UserIdentity.Headoffice)
                return list;
            else
                return list.Where(a => a.Key == UserIdentity?.Factory?.ToString()).ToList();
        }

        /// <summary>
        /// 用于页面获取一些基础信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        [HttpPost("PageInitial")]

        public async Task<IActionResult> PageInitialAsync(PageInitialInputModel model)
        {
            List<KeyValue<string, List<KeyValue<string, string>>>> dataDicList = null;
            List<KeyValue<string, bool>> authorizeList = null;
            List<KeyValue<string, string>> factoryList = null;
            List<KeyValue<string, string>> factoryListAll = null;

            //获取字典信息
            if (model.CategoryCodeArray != null)
                dataDicList = _dataDicHelp.GeDataDicValueByCategory(model.CategoryCodeArray);
            //获取权限信息
            if (model.FunctionCodeArray != null)
                authorizeList = await base.AuthorizeListAsync(model.FunctionCodeArray);

            //
            if (model.IsGetFactoryList)
                factoryList = GetFactoryList();
            if (model.IsGetFactoryAllList)
                factoryListAll = GetFactoryListAll();

            return this.JsonSuccess(new
            {
                dataDicList,//字典信息
                authorizeList, //权限信息
                factoryList, //工厂信息
                factoryListAll
            });
        }

        /// <summary>
        /// 获取所有除总部外的其他工厂信息当前登录人为总部，全部返回，否则仅返回对应工厂
        /// </summary>
        /// <returns></returns>
        [HttpGet("FactoryList")]
        public IActionResult FactoryList()
        {
            var factoryList = this.GetFactoryList();
            return this.JsonSuccess(factoryList);
        }

        /// <summary>
        /// 获取所有工厂信息，包括总部，当前登录人为总部，全部返回，否则仅返回对应工厂
        /// </summary>
        /// <returns></returns>
        [HttpGet("FactoryListAll")]
        public IActionResult FactoryListAll()
        {
            var factoryList = this.GetFactoryListAll();
            return this.JsonSuccess(factoryList);
        }

        /// <summary>
        /// 获取指定controller内所有已有的FunctionCode
        /// </summary>
        /// <param name="controllerName"></param>
        /// <returns></returns>
        [HttpGet("FunctionCodeHelp")]
        public IActionResult FunctionCodeHelp(string controllerName)
        {
            List<string> functionCodes = new List<string>();
            var type = ControllerReflectHelp.GetControllerType(controllerName);
            PermissionHelp permissionHelp = new PermissionHelp(type);
            functionCodes.AddRange(permissionHelp.GetFunctionCode());
            return this.JsonSuccess(functionCodes);
        }


    }
}